package org.xtense.rcp.editors.action;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.actions.ActionGroup;
import org.xtense.data.DBConfig;
import org.xtense.data.DBManager;
import org.xtense.engine.actions.QueryAction;
import org.xtense.engine.actions.UpdateAction;
import org.xtense.engine.actions.XAction;
import org.xtense.rcp.editors.XmlEditor;
import org.xtense.rcp.util.ImagesContext;
import org.xtense.rcp.util.XmlTreeUtil;

public class XmlEditorActionGroup extends ActionGroup {
	private Shell shell;
	private TreeViewer treeViewer;
	private XmlEditor editor;
	private String docName = null;
	private int docID = -1;
	private Action openAction = new OpenAction();
	private Action saveAction = new SaveAction();
	private Action storeAction = new StoreAction();

	public XmlEditorActionGroup(Shell shell, TreeViewer tv, XmlEditor editor) {
		this.shell = shell;
		this.treeViewer = tv;
		this.editor = editor;
	}
	
	public void setDocID(int docId) {
		this.docID = docId;
	}

	public void fillActionToolBars(ToolBarManager toolBarManager,
			boolean isFromFile) {
		toolBarManager.removeAll();
		toolBarManager.add(openAction);
		toolBarManager.add(saveAction);
		
		if (!isFromFile) {
			toolBarManager.add(new RefreshAction());
			toolBarManager.add(new UIUpdateAction());
		} else {
			toolBarManager.add(storeAction);
		}
		toolBarManager.update(true);
	}

	private class OpenAction extends Action {
		public OpenAction() {
			setText("&Open");
			setImageDescriptor(ImagesContext.getImageDescriptor(ImagesContext.Open));
		}

		public void run() {
			FileDialog fileDialog = new FileDialog(shell,SWT.OPEN);
			fileDialog.setFilterExtensions(new String[] { "*.xml" });
			String path = fileDialog.open();
			docName = XmlTreeUtil.createXMLTree(treeViewer, path);
			editor.updateToolBar(true, -1);
			editor.setDirty(false);
			editor.setPartName(docName);
		}
	}

	private class SaveAction extends Action {

		public SaveAction() {
			setText("&Save");
			setImageDescriptor(ImagesContext.getImageDescriptor(ImagesContext.Save));
		}

		public void run() {
			FileDialog fileDialog = new FileDialog(shell,SWT.SAVE);
			fileDialog.setFilterExtensions(new String[] { "*.xml" });
			String path = fileDialog.open();
			XmlTreeUtil.saveXMLTree(treeViewer, path);
			editor.setDirty(false);
		}
	}

	private class StoreAction extends Action {

		public StoreAction() {
			setText("Sto&re");
			setImageDescriptor(ImagesContext.getImageDescriptor(ImagesContext.Store));
		}

		public void run() {
			XmlTreeUtil.storeXml(treeViewer, docName);
			editor.setDirty(false);
		}
	}
	
	private class UIUpdateAction extends Action {
		public UIUpdateAction() {
			setText("&Update");
			setImageDescriptor(ImagesContext
					.getImageDescriptor(ImagesContext.Update));
		}

		public void run() {
			if (docID > 0) {
			 UpdateAction update = UpdateAction.getInstance(docID);
				update.execute(null);
			}
			editor.setDirty(false);
		}
	}
	
	private class RefreshAction extends Action {
		public RefreshAction() {
			setText("Refresh");
			setImageDescriptor(ImagesContext
					.getImageDescriptor(ImagesContext.Refresh24));
		}

		public void run() {
			XAction query = new QueryAction(new DBManager(DBConfig.getInstance(
					false, null)), docID);

			editor.showResult(query.execute("/"));
			editor.setDirty(false);
		}
	}
}
